<?php
/**
 * 栏目控制器
 * @author liuyemin
 *
 */
class CategoryController extends HomeController{
	
	public function filters(){
		return array(
				array(
						'system.web.widgets.COutputCache + index',
						'duration'=>24*60*60,
						'varyByParam'=>array('name')
				)
		);
	}
	
	public function init(){
		parent::init();
	}
	
	public function actionIndex($name){
		$article = Article::model();
		$category = Category::model();
		$cids = $category->getCids($name);
		if(empty($cids)){
			$this->error('错误的栏目号');
		}

		//创建搜索条件
		$cdb = new CDbCriteria();
		$cdb->select = 'article_id,title,post_time';
		$cdb->condition = 'category_id in ('.$cids.')';
		$cdb->order = 'post_time DESC';
		
		//执行SQL
		$total = $article->count($cdb);
		
		//分页计算
		$pager = new CPagination($total);
		$pager->pageSize = 34;
		$pager->applyLimit($cdb);
		
		//栏目分页数据
		$list = $article->findAll($cdb);
		
		//获取当前栏目的信息
		$cateInfo = $category->findByAttributes(array('name'=>$name));
		$this->_title = $cateInfo->title . '-' . Yii::app()->name;
		$this->_keywords = $cateInfo->keywords . '-' . Yii::app()->name;
		$this->_description = $cateInfo->description;

		$data = array(
				'list'=> $list,
				'pages'=>$pager,
				'cateInfo'=>$cateInfo
		);
		$this->render('index', $data);
	}
}